package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class BibliotecaApp {

    private static BooksRepository booksRepository = new BooksRepository();
    private static MovieRepository movieRepository = new MovieRepository();
    private static UserRepository userRepository = new UserRepository();

    public static void main(String[] args) throws IOException {
        welcomeMessage();

        // User Login
        userRepository.createUsers();
        User user = userLogin();

        //Creating repositories of books and movies
        booksRepository.createLibrary();
        movieRepository.createMovies();
        Book book;
        Movie movie;

        //Main menu
        int mainMenuOption;
        do {
            mainMenu();
            mainMenuOption = validOptionsMenu("mainMenu");
            switch (mainMenuOption) {
                case 1:
                    //List books
                    listBooks();
                    checkoutBookMenu();
                    int optionBook = validOptionsMenu("checkOutMenu");
                    if(optionBook != 0) {
                        book = searchBookById();
                        if (optionBook == 1) {
                            checkoutBook(book, user);
                        } else if (optionBook == 2) {
                            returnBook(book, user);
                        }
                    }
                    break;
                case 2:
                    //Return book
                    book = searchBookById();
                    returnBook(book, user);
                    break;
                case 3:
                    //List movies
                    listMovies();
                    checkoutMovieMenu();
                    int optionMovie = validOptionsMenu("checkOutMenu");

                    if(optionMovie != 0) {
                        movie = searchMovieById();
                        if (optionMovie == 1) {
                            checkoutMovie(movie);
                        }
                    }
                    break;
                case 4:
                    //User information
                    userInformation(user);
            }
        }while(mainMenuOption != 0);
    }

    public static void welcomeMessage() {
        System.out.println("Welcome to Bangalore Public Library");
    }

    //Main menu options
    public static void mainMenu() throws IOException {
        System.out.println("\nPress the number to choose");
        System.out.println("1 - List Books");
        System.out.println("2 - Return Book");
        System.out.println("3 - List Movies");
        System.out.println("4 - User Information");
        System.out.println("0 - Quit");
    }

    //Method that returns an valid option for the menu
    public static String getOption(ArrayList validOptions) throws IOException {
        String answer = null;
        do {
            answer = readConsole();
            if (validOptions.contains(answer))
                return answer;
            else {
                System.err.println("Select a valid option!");
            }
        }while(!validOptions.contains(answer));
        return answer;
    }

    public static int validOptionsMenu(String menu) throws IOException {
        ArrayList options = new ArrayList();
        if(menu.equals("mainMenu")){
            options.add("0");
            options.add("1");
            options.add("2");
            options.add("3");
            options.add("4");
            int optionMenu = Integer.parseInt(getOption(options));
            return optionMenu;
        }else if(menu.equals("checkOutMenu")){
            options.add("0");
            options.add("1");
            options.add("2");
            int optionMenu = Integer.parseInt(getOption(options));
            return optionMenu;
        }else if(menu.equals("checkoutMovieMenu")){
            options.add("0");
            options.add("1");
            int optionMenu = Integer.parseInt(getOption(options));
            return optionMenu;
        }
        return -1;
    }

    public static void listBooks() {
        booksRepository.listBooks();
    }

    public static void listMovies() {
        movieRepository.listMovies();
    }

    //Checkout Book Menu
    public static void checkoutBookMenu() {
        System.out.println("\nPress the number to choose");
        System.out.println("1 - Check out a book");
        System.out.println("2 - Return a book");
        System.out.println("0 - Return to Main Menu");
    }

    //Checkout Movie Menu
    public static void checkoutMovieMenu() {
        System.out.println("\nPress the number to choose");
        System.out.println("1 - Check out a movie");
        System.out.println("0 - Return to Main Menu");
    }

    public static Book searchBookById() throws IOException {
        System.out.println("\nEnter the ID of the book:");
        String id = readConsole();
        Book book = booksRepository.searchBook(id);
        return book;
    }

    public static Movie searchMovieById() throws IOException {
        System.out.println("\nEnter the ID of the movie:");
        String id = readConsole();
        Movie movie = movieRepository.searchMovie(id);
        return movie;
    }

    public static boolean checkoutBook(Book book, User user) throws IOException {
        if (book != null && booksRepository.bookAvailability(book)) {
            booksRepository.checkOutBook(book, user);
            System.out.println("Thank you! Enjoy the book.");
            return true;

        } else {
            System.out.println("That book is not available.");
            return false;
        }
    }

    public static boolean returnBook(Book book, User user) throws IOException {
        if (book != null && !booksRepository.bookAvailability(book)) {
            booksRepository.returnBook(book, user);
            System.out.println("Thank you for returning the book.");
            return true;
        } else {
            System.out.println("That is not a valid book to return.");
            return false;
        }
    }

    public static boolean checkoutMovie(Movie movie) throws IOException {
        if (movie != null && movieRepository.movieAvailability(movie)) {
            movieRepository.checkOutMovie(movie);
            System.out.println("Thank you! Enjoy the book.");
            return true;

        } else {
            System.out.println("That book is not available.");
            return false;
        }
    }

    //Read information entered by the user at the console
    public static String readConsole() throws IOException{
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String outputConsole = bufferRead.readLine();
        return outputConsole;
    }

    //User login
    public static User userLogin() throws IOException {
        User user = null;
        while(user == null) {
            System.out.println("Enter your library number:");
            String libraryNumber = readConsole();
            System.out.println("Enter your password:");
            String password = readConsole();
            user = userRepository.searchUser(libraryNumber, password);
            if (user == null) {
                System.err.println("Password or library number does not exist");
            }
        }
        return user;
    }

    //Print user informations
    public static void userInformation(User user){
        userRepository.showUserInformation(user);
    }

}
